//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QSqlRecord>
#include "CMyTable.h"
#include "CMyComboBox.h"
#include "CMyTableWidgetItem.h"
#include "CComboResultFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CComboResultFrm::CComboResultFrm(QString tableName, QString champ, QWidget *parent, int indexChampRet, QString where) : QDialog(parent, Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint)
{
	setupUi(this);
	
	_table=tableName;
	_champ=champ;
	_result="";
	findText="";
	_indexChampRet=indexChampRet;
	_where=where;
	
	fontTemplate="<font color='%1'>%2</font>";
	
	table->setContextMenuPolicy(Qt::CustomContextMenu);
	
	lblSaisie->setText(fontTemplate.arg("#0000FF", ""));
	
	loadChamps();
	loadDatas();
	
	makeMenu();
}
//------------------------------------------------------------------------------
void CComboResultFrm::loadChamps(void)
{
	QVector<CMySQL::SChampRecherche> list=CMySQL::getInstance()->getChampsRecherche(_table, false);
	QVector<CMySQL::SChampRecherche>::iterator i;
	for(i=list.begin();i!=list.end();i++)
	{
		listChamp.append((*i).champNom);
		header.append((*i).champDescription);
		
		if((*i).champNom == _table+"."+_champ)
		{
			_champDescription=(*i).champDescription;
		}
	}
}
//------------------------------------------------------------------------------
void CComboResultFrm::loadDatas(void)
{
	QString requete="SELECT ";
	QString champs;
	QSqlQuery *query;
	int i, recAff, X, Y, numCols;
	QString recAffS;
	
	for(i=0;i<listChamp.size();i++)
	{
		if(champs != "") champs+=", ";
		champs+=listChamp.at(i);
	}
	
	requete+=champs+" FROM "+_table+" ";
	if(_where != "")
	{
		requete+="WHERE "+_where;
	}
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		recAff=query->size();
	
		table->setRowCount(recAff);
		table->setColumnCount(numCols=query->record().count());	
		table->setSortingEnabled(false);
		table->setHorizontalHeaderLabels(header);
		
		Y=0;
		while(query->next())
		{
			for(X=0;X<numCols;X++)
			{
				CMyTableWidgetItem *item;
				QSqlRecord rec=query->record();
				
				item=new CMyTableWidgetItem(rec.value(X));
				
				table->setItem(Y, X, item);
			}
			Y++;
		}	
		recAffS.setNum(recAff);
		setWindowTitle(_champDescription+tr(" (")+recAffS+tr(")"));
		table->resizeColumnsToContents();
		table->setSortingEnabled(true);
		table->sortByColumn(0, Qt::AscendingOrder);
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CComboResultFrm::keyPressEvent(QKeyEvent * event)
{
	QString color=table->getAscending()==Qt::AscendingOrder ? "#0000FF" : "#FF0000";
	
	switch(event->key())
	{
		case Qt::Key_Backspace:
			if(!findText.isEmpty())
			{
				findText.truncate(findText.length()-1);
				lblSaisie->setText(fontTemplate.arg(color, findText));
				table->find(findText);
			}
			break;
		case Qt::Key_Delete:
			findText="";
			lblSaisie->setText(fontTemplate.arg(color, findText));
			table->find(findText);
			break;
		case Qt::Key_Enter:
		case Qt::Key_Return:
			makeResults(table->currentRow());
			accept();
			break;
		default:
			if(event->text() >= " ")
			{
				findText+=event->text();
				lblSaisie->setText(fontTemplate.arg(color, findText));
				table->find(findText);
			}
			break;
	}
	QDialog::keyPressEvent(event);
}
//-----------------------------------------------------------------------------------
void CComboResultFrm::makeMenu(void)
{
	popupMenu=new QMenu(table);
	
	actActionExport=new QAction(tr("&Exporter le contenu de la table"), this);
	connect(actActionExport, SIGNAL(triggered()), this, SLOT(onActionExport_triggered()));
	
	popupMenu->addAction(actActionExport);
}
//------------------------------------------------------------------------------
void CComboResultFrm::makeResults(int row) {
	int i;
	_result=table->item(row, _indexChampRet)->text();
	for(i=0;i<table->columnCount();i++) {
		QString key=listChamp.at(i);
		results.insert(key, table->item(row, i)->text());
	}
}
//------------------------------------------------------------------------------
void CComboResultFrm::on_table_ascendingChange(Qt::SortOrder ascending)
{
	lblSaisie->setText(fontTemplate.arg(ascending==Qt::AscendingOrder ? "#0000FF" : "#FF0000", findText));
	table->find(findText);
}
//------------------------------------------------------------------------------
void CComboResultFrm::on_table_columnChange(int col)
{
	findText="";
	lblSaisie->setText(fontTemplate.arg("#0000FF", findText));
	table->find(findText);
}
//------------------------------------------------------------------------------
void CComboResultFrm::on_table_valid(int row, int col)
{
	makeResults(row);
	accept();
}
//------------------------------------------------------------------------------
void CComboResultFrm::on_table_cellDoubleClicked(int row, int col)
{
	makeResults(row);
	accept();
}
//------------------------------------------------------------------------------
void CComboResultFrm::on_table_customContextMenuRequested(const QPoint & pos)
{
	QPoint p=pos;
	
	p.rx()+=table->verticalHeader()->width();
	p.ry()+=table->horizontalHeader()->height();
	
	popupMenu->popup(table->mapToGlobal(p));
}
//------------------------------------------------------------------------------
void CComboResultFrm::onActionExport_triggered(void)
{
	table->exportCSV(_table);
}
//------------------------------------------------------------------------------
